#!/bin/bash

echo "### igh Ethercat 驱动环境搭建"

# 非实时网卡使用
ifconfig eth0 down

rmmod ec_generic
rmmod ec_master

rm -rf /usr/local/out_igh/

mkdir -p /usr/local/out_igh/
cp -rf ./* /usr/local/out_igh/

cp -rf ./driver/*.ko /lib/modules/5.10.160-rt89/
cp -rf ./lib/libethercat.so.1.2.0 /lib/
ln -s /lib/libethercat.so.1.2.0 /lib/libethercat.so
ln -s /lib/libethercat.so.1.2.0 /lib/libethercat.so.1

ln -s /usr/local/out_igh/bin/ethercat /bin/ethercat
ln -s /usr/local/out_igh/etc/init.d/ethercat /etc/init.d/ethercat
ln -s /usr/local/out_igh/etc/ethercat.conf /etc/ethercat.conf
mkdir -p /etc/sysconfig
ln -s /usr/local/out_igh/etc/sysconfig/ethercat /etc/sysconfig/ethercat

ln -s /usr/local/out_igh/sbin/ethercatctl /sbin/ethercatctl


echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.rules

# main_devices改成具体的MAC地址
# insmod /lib/modules/5.10.160-rt89/ec_master.ko main_devices=86:0b:eb:d3:cf:88
# insmod /lib/modules/5.10.160-rt89/ec_sunxi_gmac.ko

if [ ! -e /bin/ip ]; then
    echo " /bin/ip are not exist"
    cp -f /sbin/ip /bin/ip
fi

# 加载驱动
depmod -a
modprobe ec_master main_devices=86:0b:eb:d3:cf:88
sleep 1
/etc/init.d/ethercat start


# 非实时网卡使用
ifconfig eth0 up
sleep 1
modprobe ec_generic

chmod +x /root/my*
echo "### igh Ethercat 驱动环境搭建完成"

# sleep 1
# ethercat master
# echo "### igh Ethercat 主站状态打印"
